package com.tbit.main.controller;

import cn.hutool.core.collection.CollUtil;
import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.SysConstant;
import com.tbit.main.h2.pojo.ParkPoint;
import com.tbit.main.pojo.*;
import com.tbit.main.service.*;
import com.tbit.main.util.Assert;
import com.tbit.main.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 站点设备管理
 *
 * @author LMY
 * @create 2021-12-27 13:40
 */
@RestController
@RequestMapping("/parkPointDevice")
public class ParkPointDeviceController {

    @Autowired
    private GeoreGionService georeGionService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ParkPointService parkPointService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private BleBeaconService bleBeaconService;
    @Autowired
    private ParkPointBoundService parkPointBoundService;

    /**
     * 根据区域查询
     *
     * @param token
     * @param accountId
     * @param mapType
     * @param state
     * @param time
     * @param regionId
     * @return
     */
    @RequestMapping("/getAccountId")
    public JsonResult getByAccountId(String token, Integer accountId, Integer mapType, Integer state, Integer time,
                                     @RequestParam(value = "regionId", required = false) List<Integer> regionId) {

        AccountUser accountUserToken = tokenService.getAccount(token);
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        if (CollUtil.isEmpty(regionId)) {
            regionId = georeGionService.getByAccountUserId(accountUserToken.getAccountUserId());
        }

        List<ParkPoint> parkPoints = new LinkedList<>();
        if (CollUtil.isNotEmpty(regionId)) {
            parkPoints = parkPointService.getByRegionId(regionId);
        } else {
            parkPoints = parkPointService.getByAccountId(accountId);
        }

        if (parkPoints != null && parkPoints.size() > 0) {
            List<Integer> parkPointIds = parkPoints.stream().map(ParkPoint::getParkPointId).collect(Collectors.toList());
            /**站点设备关系*/
            Map<Integer, List<ParkPointDeviceCount>> mapCount = new HashMap<>();

            /**道钉集合*/
            List<ParkPointBoundCount> balCounts = new LinkedList<>();
            /**rfid集合*/
            List<ParkPointBoundCount> boundCounts = new LinkedList<>();

            if (state == null && time == null) {
                /**查询总数*/
                balCounts = bleBeaconService.getAllId(parkPointIds);

                boundCounts = parkPointBoundService.getByAccountId(parkPointIds, null);
            } else if (state == 0) {
                /**根据异常类型查询*/
                balCounts = bleBeaconService.getByStateId(null, parkPointIds);

                boundCounts = parkPointBoundService.getByStateId(null, null, parkPointIds);
            } else {
                /**根据时间查询*/
                balCounts = bleBeaconService.getTimeCount(DateTimeUtil.getDayAgoTime(time), parkPointIds);

                boundCounts = parkPointBoundService.getTimeCount(DateTimeUtil.getDayAgoTime(time), null, parkPointIds);
            }

            /**道钉*/
            for (ParkPointBoundCount parkPointBoundCount : balCounts) {
                /**设备数*/
                ParkPointDeviceCount parkPointDeviceCount = new ParkPointDeviceCount();
                parkPointDeviceCount.setType(parkPointBoundCount.getType());
                parkPointDeviceCount.setTotal(parkPointBoundCount.getTotal());
                parkPointDeviceCount.setState(parkPointBoundCount.getState());

                if (mapCount.containsKey(parkPointBoundCount.getParkPointId())) {
                    mapCount.get(parkPointBoundCount.getParkPointId()).add(parkPointDeviceCount);
                } else {
                    List<ParkPointDeviceCount> parkPointDeviceCounts = new LinkedList<>();
                    parkPointDeviceCounts.add(parkPointDeviceCount);
                    mapCount.put(parkPointBoundCount.getParkPointId(), parkPointDeviceCounts);
                }
            }

            /**rifd*/
            for (ParkPointBoundCount parkPointBoundCount : boundCounts) {

                /**设备数*/
                ParkPointDeviceCount parkPointDeviceCount = new ParkPointDeviceCount();
                parkPointDeviceCount.setType(parkPointBoundCount.getType());
                parkPointDeviceCount.setTotal(parkPointBoundCount.getTotal());
                parkPointDeviceCount.setState(parkPointBoundCount.getState());

                if (mapCount.containsKey(parkPointBoundCount.getParkPointId())) {
                    mapCount.get(parkPointBoundCount.getParkPointId()).add(parkPointDeviceCount);
                } else {
                    List<ParkPointDeviceCount> parkPointDeviceCounts = new LinkedList<>();
                    parkPointDeviceCounts.add(parkPointDeviceCount);
                    mapCount.put(parkPointBoundCount.getParkPointId(), parkPointDeviceCounts);
                }
            }

            for (ParkPoint parkPoint : parkPoints) {
                parkPoint.setParkPointDeviceCount(mapCount.get(parkPoint.getParkPointId()));
            }
        }

        /** 校准 */
        calibrateService.celibrateParkPoint(parkPoints, mapType);

        return JsonResult.succ(parkPoints);
    }

    /**
     * 根据站点，异常查询
     *
     * @param token
     * @param parkPointId
     * @return
     */
    @RequestMapping("/abnormal")
    public JsonResult getAbnormal(String token, Integer parkPointId, Integer time) {

        List<ParkPointDeviceCount> parkPointDeviceCounts = new LinkedList<>();

        List<Integer> parkPointIds = new LinkedList<>();
        parkPointIds.add(parkPointId);

        /**根据异常类型查询*/
        List<ParkPointBoundCount> balCounts = bleBeaconService.getByStateId(null, parkPointIds);
        List<ParkPointBoundCount> boundCounts = parkPointBoundService.getByStateId(null, null, parkPointIds);

        /**根据时间查询*/
        if (time != null) {
            List<ParkPointBoundCount> bleBeacons = bleBeaconService.getTimeCount(DateTimeUtil.getDayAgoTime(time), parkPointIds);
            List<ParkPointBoundCount> parkPointBounds = parkPointBoundService.getTimeCount(DateTimeUtil.getDayAgoTime(time), null, parkPointIds);

            if (bleBeacons != null && bleBeacons.size() > 0) {
                for (ParkPointBoundCount parkPointBoundCount : bleBeacons) {
                    ParkPointDeviceCount parkPointDeviceCount = new ParkPointDeviceCount();
                    parkPointDeviceCount.setType(parkPointBoundCount.getType());
                    parkPointDeviceCount.setTotal(parkPointBoundCount.getTotal());
                    parkPointDeviceCounts.add(parkPointDeviceCount);
                }
            }
            if (parkPointBounds != null && parkPointBounds.size() > 0) {
                for (ParkPointBoundCount parkPointBoundCount : parkPointBounds) {
                    ParkPointDeviceCount parkPointDeviceCount = new ParkPointDeviceCount();
                    parkPointDeviceCount.setType(parkPointBoundCount.getType());
                    parkPointDeviceCount.setTotal(parkPointBoundCount.getTotal());
                    parkPointDeviceCounts.add(parkPointDeviceCount);
                }
            }

        }


        /**道钉*/
        if (balCounts != null && balCounts.size() > 0) {
            for (ParkPointBoundCount parkPointBoundCount : balCounts) {
                ParkPointDeviceCount parkPointDeviceCount = new ParkPointDeviceCount();
                parkPointDeviceCount.setType(parkPointBoundCount.getType());
                parkPointDeviceCount.setTotal(parkPointBoundCount.getTotal());
                parkPointDeviceCount.setState(parkPointBoundCount.getState());
                parkPointDeviceCounts.add(parkPointDeviceCount);
            }
        }

        /**rifd*/
        if (boundCounts != null && boundCounts.size() > 0) {
            for (ParkPointBoundCount parkPointBoundCount : boundCounts) {
                ParkPointDeviceCount parkPointDeviceCount = new ParkPointDeviceCount();
                parkPointDeviceCount.setType(parkPointBoundCount.getType());
                parkPointDeviceCount.setTotal(parkPointBoundCount.getTotal());
                parkPointDeviceCount.setState(parkPointBoundCount.getState());
                parkPointDeviceCounts.add(parkPointDeviceCount);
            }
        }


        return JsonResult.succ(parkPointDeviceCounts);
    }

    /**
     * 根据站点，时间查询
     *
     * @param token
     * @param parkPointId
     * @param time
     * @return
     */
    @RequestMapping("/time")
    public JsonResult getTime(String token, Integer parkPointId, Integer time) {

        List<Integer> parkPointIds = new LinkedList<>();
        parkPointIds.add(parkPointId);

        Integer total = 0;
        List<BleBeacon> bleBeacons = bleBeaconService.getTime(DateTimeUtil.getDayAgoTime(time), parkPointIds, null, null);
        if (bleBeacons != null && bleBeacons.size() > 0) {
            total = bleBeacons.size();
        }
        List<ParkPointBound> parkPointBounds = parkPointBoundService.getTime(DateTimeUtil.getDayAgoTime(time), null, parkPointIds, null, null);
        if (parkPointBounds != null && parkPointBounds.size() > 0) {
            total = parkPointBounds.size();
        }

        return JsonResult.succ(total);
    }
}
